package com.newland;

import com.newland.util.DBUtils;
import com.newland.util.MyUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Properties;

/**
 *Description:
 *Version:1.0.0
 *@author shihb
 *@date 2020/4/1 9:29
 */
public class DataXFile2Oracle {

  public static void main(String[] args) {
    System.out.println("DataXFile2Oracle Start");
    if(args.length>0){
      MyUtils.loadConfigFile(args[0]);
    }else {
      MyUtils.loadConfigFile();
    }
    try {
      Properties prop= MyUtils.getConfig();
      Integer waitingSecond = Integer.parseInt(prop.getProperty("waitingSecond"));
      System.out.println("waitingSecond:"+waitingSecond);
      File resultFile = new File(prop.getProperty("targetDir"));
      long start = System.currentTimeMillis();
      File [] fileArr = null;
      boolean flag = true;
      do {
        fileArr = resultFile.listFiles();
        if(fileArr.length>=3){
          flag=false;
          break;
        }
        Thread.sleep(1000);
      }while ((System.currentTimeMillis()-start)/1000 < waitingSecond);
      if (flag){
        System.exit(0);
      }
      String sumTraceCode  = prop.getProperty("sumTraceCode");
      String sumSearchLog = prop.getProperty("sumSearchLog");
      String traceCodeList = prop.getProperty("traceCodeList");
      for( File file:fileArr){
        if(file.getName().startsWith(traceCodeList)){
          // 赋码明细表
          System.out.println("Execute "+traceCodeList);
          insertDetail(file,traceCodeList);
        }else if(file.getName().startsWith(sumSearchLog)){
          // 扫码数
          System.out.println("Execute"+sumSearchLog);
          insertCount(file,sumSearchLog);
        }else if(file.getName().startsWith(sumTraceCode)){
          // 赋值数
          System.out.println("Execute "+sumTraceCode);
          insertCount(file,sumTraceCode);
        }
      }
    }catch (Exception e){
      e.printStackTrace();
    }finally {
      DBUtils.close();
    }
  }

  private static void insertDetail(File file, String table) throws Exception {
    BufferedReader  br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
    String line;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    String startDate = sdf.format(System.currentTimeMillis());
    ArrayList<String> sqlList= new ArrayList<String>();
    while((line=br.readLine())!=null){
      StringBuilder sb = new StringBuilder();
      sb.append("insert into ").append(table).append(" values(");
      sb.append("'").append(startDate).append("',");
      String[] cols = line.split("\1",-1);
      for (int i = 0; i < cols.length ; i++) {
        if(i==11 || i==19 || i==20){
          sb.append("to_date('").append(cols[i]).append("','yyyymmdd hh24miss'),");
        }else {
          sb.append("'").append(cols[i]).append("',");
        }
      }
      sb.delete(sb.length()-1,sb.length());
      sb.append(")");
      sqlList.add(sb.toString());
    }
//    System.out.println("delete from "+table);
//    DBUtils.executeSql("delete from "+table);
    System.out.println(sqlList.get(0).toString());
    DBUtils.batchExecuteSql(sqlList);
    br.close();
    file.delete();
  }

  private static void insertCount(File file, String table) throws Exception{
    BufferedReader  br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String sqlDate = sdf.format(System.currentTimeMillis());
    String count=null;
    String date = sqlDate.substring(0,4)+sqlDate.substring(5,7)+sqlDate.substring(8,10);
    while((count=br.readLine())!=null){
      StringBuilder sb = new StringBuilder();
      sb.append("insert into ").append(table).append("(STAT_DATE,TRACE_CODE_CNT,IMP_TIME) values(");
      sb.append("'").append(date).append("',");
      sb.append("'").append(count).append("',");
      sb.append("to_date('").append(sqlDate).append("','yyyy-mm-dd hh24:mi:ss')");
      sb.append(")");
//      System.out.println("delete from "+table);
//      DBUtils.executeSql("delete from "+table);
      System.out.println(sb.toString());
      DBUtils.executeSql(sb.toString());
    }
    br.close();
    file.delete();
  }
}
